﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Contract;
using WHC.OrderWater.Business;
using WHC.Hotel.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditDiscount : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditDiscount()
        {
            InitializeComponent();
        }

        private void InitProductName()
        {
            this.cmbProduct.Items.Clear();
            List<ProductInfo> list = BLLFactory<Product>.Instance.GetAll();
            foreach (ProductInfo info in list)
            {
                CListItem item = new CListItem(info.ProductName, info.ID);
                this.cmbProduct.Items.Add(item);
            }
        }

        private void FrmEditArea_Load(object sender, EventArgs e)
        {
            InitProductName();

            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                QuantityDiscountInfo info = BLLFactory<QuantityDiscount>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtQuantity.Value = Convert.ToDecimal(info.Quantity);
                    txtDiscount.Value = Convert.ToDecimal(info.Discount * 100);
                    txtNote.Text = info.Note;
                    lblCreateDate.Text = info.LastUpdated.ToString();

                    int index = this.cmbProduct.FindStringExact(info.ProductName);
                    this.cmbProduct.SelectedIndex = index; 
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }

        private void SetInfo(QuantityDiscountInfo info)
        {
            info.Quantity = Convert.ToInt32(txtQuantity.Value);
            info.Discount = Convert.ToDouble(txtDiscount.Value / 100);
            info.LastUpdated = DateTime.Now;
            info.Note = txtNote.Text;

            if (this.cmbProduct.SelectedItem != null)
            {
                CListItem item = this.cmbProduct.SelectedItem as CListItem;
                info.ProductName = item.Text;
                info.Product_ID = item.Value;
            }            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cmbProduct.SelectedItem == null)
            {
                MessageUtil.ShowTips("请选择产品名称");
                this.cmbProduct.Focus();
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                QuantityDiscountInfo info = BLLFactory<QuantityDiscount>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<QuantityDiscount>.Instance.Update(info, info.ID);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                QuantityDiscountInfo info = new QuantityDiscountInfo();
                SetInfo(info);
                info.ID = Guid.NewGuid().ToString();

                try
                {
                    bool succeed = BLLFactory<QuantityDiscount>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
